﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

enum Moving
{
    NONE,
    GOING,
    RETURNING
}

public class NPC : Characters
{
    private string name;
    private Texture2D texture;
    private Texture2D face;
    private List<int> start;
    private Dictionary<int, Dialog> dialogos;
    private int map;
    private int area;
    private List<Condition> need;
    bool interact = true;

    private List<Thought> thoughtList;

    public List<Thought> ThoughtList
    {
        get { return thoughtList; }
        set { thoughtList = value; }
    }

    Vector2 move = Vector2.Zero;

    Moving moving = Moving.NONE;

    public Vector2 Move
    {
        get { return move; }
        set 
        {
            move = value;
        }
    }

    public List<Condition> Need
    {
        get { return need; }
        set { need = value; }
    }

    public bool Interact
    {
        get
        {
            if (!interact)
                return false;
            if (GameSession.CheckAllConditions(need) || !GetSentence(thoughtList).Equals(string.Empty))
                return true;

            return false;
        }
        set { interact = value; }
    }

    public bool HasDialog
    {
        get
        {
            if (!interact)
                return false;
            if (GameSession.CheckAllConditions(need))
                return true;

            return false;
        }
        set { interact = value; }
    }

    public int Area
    {
        get { return area; }
        set { area = value; }
    }

    public int Map
    {
        get { return map; }
        set { map = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public Texture2D Sprite
    {
        get { return texture; }
        set 
        {
            texture = value;
            /*int wid = value.Width / 3;
            int hei = value.Height / 4;
            int aux = wid * hei;
            Color[] tex = new Color[aux];
            value.GetData(0, new Rectangle(wid, hei * 2, wid, hei), tex, 0, aux);
            texture = new Texture2D(ScreenManager.Graphics.GraphicsDevice, wid, hei);
            texture.SetData(tex);*/
            CreateAnimation(texture);
            //UpdateTexture(texture);
        }
    }

    public Texture2D Face
    {
        get { return face; }
        set { face = value; }
    }

    public Dictionary<int, Dialog> Dialogos
    {
        get { return dialogos; }
        set { dialogos = value; }
    }

    public List<int> Start
    {
        get { return start; }
        set { start = value; }
    }

    public NPC() : base(0,0, Vector2.Zero)
    {
        area = 70;
        start = new List<int>();
        need = new List<Condition>();
        dialogos = new Dictionary<int, Dialog>();
        thoughtList = new List<Thought>();
    }

    public void Draw(GameTime gameTime, SpriteBatch spriteBatch, int px, int py)
    {
        //spriteBatch.Draw(texture,new Vector2(Globals.ScreenWidth2 - (px - position.X), Globals.ScreenHeight2 - (py - position.Y)), Color.White);

        sprite.Draw(gameTime, spriteBatch, new Vector2(Globals.ScreenWidth2 - (px - position.X), Globals.ScreenHeight2 - (py - position.Y)), SpriteEffects.None); 
        //sprite.Draw(gameTime, spriteBatch, new Vector2(position.X + px, position.Y + py), flip);
    }

    public string GetSentence(List<Thought> thoughtList)
    {
        //thoughtNum = -1;
        for (int i = thoughtList.Count - 1; i >= 0; i--)
        {
            if (GameSession.CheckAllConditions(thoughtList[i].Need))
            {
                //thoughtNum = i;
                return thoughtList[i].Sentence;
            }
        }

        return string.Empty;
    }

    public override void Update(GameTime gameTime)
    {
        Rectangle rec = GameSession.Player.BoundingRectangle;

        Vector2 reccenter = new Vector2(rec.X + rec.Width / 2, rec.Y + rec.Height / 2);
        Vector2 npccenter = new Vector2(BoundingRectangle.X + BoundingRectangle.Width / 2,
            BoundingRectangle.Y + BoundingRectangle.Height / 2);

        if (Math.Sqrt(Math.Pow(reccenter.X - npccenter.X, 2) + Math.Pow(reccenter.Y - npccenter.Y, 2)) <= area * 2)
        {
            Velocity = Vector2.Zero;
        }
        else
        if (moving == Moving.NONE && (move.X != 0 || move.Y != 0))
            moving = Moving.GOING;
        else
            if (moving == Moving.GOING)
            {
                if(move.X != 0)
                    if (move.X + origin.X > position.X)
                    {
                        Velocity.X += 2;
                        _Direction = Direction.Right;
                    }
                    else
                    {
                        moving = Moving.RETURNING;
                    }

                if (move.Y != 0)
                    if (move.Y + origin.Y > position.Y)
                    {
                        Velocity.Y += 2;
                        _Direction = Direction.Down;
                    }
                    else
                    {
                        moving = Moving.RETURNING;
                    }
            }
            else
            {
                if (moving == Moving.RETURNING)
                {
                    if (move.X != 0)
                        if (origin.X - move.X < position.X)
                        {
                            Velocity.X -= 2;
                            _Direction = Direction.Left;
                        }
                        else
                        {
                            moving = Moving.GOING;
                        }

                    if (move.Y != 0)
                        if (origin.Y - move.Y < position.Y)
                        {
                            Velocity.Y -= 2;
                            _Direction = Direction.Up;
                        }
                        else
                        {
                            moving = Moving.GOING;
                        }
                }
            }

        base.Update(gameTime);
    }

    internal void AddThought(Thought thought)
    {
        thoughtList.Add(thought);
    }
}
